package com.csaba.swing.action;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.AbstractAction;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;

import com.csaba.connector.BankServiceFactory;
import com.csaba.connector.model.Session;
import com.csaba.connector.service.LogoutService;
import com.csaba.swing.MainWindow;
import com.csaba.swing.gui.ContextListener;
import com.csaba.swing.gui.GUIContext;
import com.csaba.swing.gui.LogoutDialog;
import com.csaba.swing.gui.util.GUIUtil;
import com.csaba.util.LoggerHelper;

@SuppressWarnings( "serial" )
public class LogoutAction extends AbstractAction implements ContextListener
{
	private final static Logger logger = LoggerHelper.getDefaultLogger();

	public LogoutAction()
	{
		super(MainWindow.getString("LogoutAction.menuItem")); //$NON-NLS-1$
		setEnabled(false);
		putValue(SMALL_ICON, GUIContext.getIcon("logout", 16)); //$NON-NLS-1$
		putValue(LARGE_ICON_KEY, GUIContext.getIcon("logout", 32)); //$NON-NLS-1$
		GUIUtil.setMnemonic(this);
		putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0));
		GUIContext.getInstance().addContextListener(this);
	}

	@Override
	public void actionPerformed( final ActionEvent e )
	{
		final GUIContext context = GUIContext.getInstance();
		if ( !context.hasActiveSession() )
			return;

		//check whether there is a session,
		if ( e == null )
		{
			doLogout(null);
		}
		else
		{
			final List<Session> sessions = context.getSessions();
			//check whether 1 session is open or more
			//close one session or open dialog
			if ( sessions.size() == 1 )
			{
				final int result = JOptionPane
						.showConfirmDialog(
								MainWindow.getFrame(),
								MainWindow.getString("LogoutAction.msg.confirmLogout"), MainWindow.getString("LogoutAction.titleLogout"), JOptionPane.YES_NO_OPTION); //$NON-NLS-1$ //$NON-NLS-2$
				if ( result == JOptionPane.NO_OPTION )
				{
					return;
				}
				doLogout(sessions.get(0));
			}
			else
			{
				final LogoutDialog dialog = new LogoutDialog(MainWindow.getFrame());
				dialog.setVisible(true);
			}
		}
	}

	@Override
	public void contextEventPerformed( final int eventType )
	{
		final boolean hasSession = GUIContext.getInstance().hasActiveSession();
		setEnabled(hasSession);
	}

	public void doLogout( final Session session )
	{
		final GUIContext context = GUIContext.getInstance();
		List<Session> sessions;
		if ( session != null )
		{
			sessions = new ArrayList<Session>();
			sessions.add(session);
		}
		else
		{
			sessions = context.getSessions();
		}

		for ( final Session oneSession : sessions )
		{
			try
			{
				final LogoutService service = BankServiceFactory.getBankService(oneSession.getBank(),
						LogoutService.class);

				new ServiceRunner(oneSession, service, MainWindow.getString(
						"LogoutAction.msg.logoutInProgress", oneSession.getCustomer())).execute(); //$NON-NLS-1$
			}
			catch ( final Exception ex )
			{
				logger.log(Level.SEVERE, "Error during logout.", ex); //$NON-NLS-1$
			}
			context.removeSession(oneSession);
			//set status bar text
			MainWindow.getStatusBar().setMessage(
					MainWindow.getString("LogoutAction.msg.successfulLogout", oneSession.getBank().getName())); //$NON-NLS-1$
		}

	}
}
